package org.example.aidemo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
@EnableRabbit
public class RabbitMQConfig {

    @Bean
    public TopicExchange emailExchange() {
        return new TopicExchange("email.exchange");
    }
    @Bean
    public Queue emailQueue() {
        return new Queue("email.queue", true); // durable=true
    }
    @Bean
    public Binding emailBinding() {
        return BindingBuilder.bind(emailQueue()).to(emailExchange()).with("email.routing.key");
    }
    @Bean
    public MessageConverter messageConverter() {
        SimpleMessageConverter converter = new SimpleMessageConverter();
        converter.setAllowedListPatterns(Arrays.asList(
                "org.example.aidemo.pojo.EmailMessage"
        ));
        return converter;
    }
}